#ifndef YMODEM_H
#define YMODEM_H

#include <unistd.h>
#include <QCoreApplication>
#include <QSerialPort>
#include <QFile>
#include <QFileDialog>
#include <QTimer>
#include <QDebug>
#include <cstdio>
#include <QMessageBox>
#include <QThread>



// Ymodem头部字段
typedef enum
{
    SOH = 0x01, //数据包的大小为 128 bytes
    STX = 0x02, //数据包的大小为 1024 bytes
    EOT = 0x04, //传输结束
    ACK = 0x06, //应答
    NAK = 0x15, //非应答
    CAN = 0x18, //取消
    C = 0x43 //启动传输

}ProtocolHeader;

#define NameLenMax 128
#define DataLen 1024

typedef struct
{
    ProtocolHeader protocolHeader; //协议头
    unsigned char FrameNumber;  //帧序列号
    unsigned char FrameNumberComplement; //帧序列号补码
    //unsigned char Filename[NameLenMax]; //文件名
    unsigned char *Filename;//文件名
    long long Fliezise; //文件大小
    unsigned char *Data; //数据
    unsigned short crcData; //crc校验数据

}YModemtype;


bool sendYmodemFile(const QString filePath, QIODevice *device);
uint16_t calculateCRC16(const uint8_t *data, size_t length);
void splitData16To8(unsigned short data16, unsigned char* high8, unsigned char* low8);
void sendStartFrame(QIODevice* device,YModemtype *YModem);
void sendDataFrame1024(QIODevice *device,YModemtype *YModem);
void sendDataFrame128(QIODevice *device,YModemtype *YModem);
void sendEndFrame(QIODevice *device,YModemtype *YModem);
uint8_t CalChecksum(uint8_t *content, uint8_t len);

#endif
